1371D - Grid-00100 - CodeForces Solution


constructive algorithms greedy implementation *1600

Please click on ads to support us..

Python Code:

def solve():
    n, k = [int(x) for x in input().split()]
    if k%n == 0: print(0)
    else: print(2)
    g = [['0']*n for _ in range(n)]
    r, c = 0, 0
    for _ in range(k):
        g[r][c] = '1'
        r, c = r+1, (c+1)%n
        if r == n:
            r = 0; c = (c+1)%n
        
    for x in g: print(''.join(x))

for _ in range(int(input())):
    solve()

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
//for math u can observe something related to gcd or something related to even or odd to draw some conclusions
//if your answer is lieing in a range then binary search is the best approach to solve that problem
//for errors check out of bound and overflow of integers
//sorting,binary search,dynamic programming,greedy are some of the most used techniques to solve the problems
//until some element it is good and then next it is bad u should use binary search
//to find the first good element or to find the first bad element we use binary search\
//if there are digits then remember that the digits are just from 0-9 so u can use two loops the same for character a-z they are just 26

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	  int n,k;
	  cin>>n>>k;
	  int p=k/n;
	  int r=k%n;
	  int a[n][n];

	  for(int i=0;i<n;i++)
	  {
	      for(int j=0;j<n;j++)
	      {
	          a[i][j]=0;
	      }
	  }
	  for(int i=0;i<n;i++)
	  {
	      int e=p;
	      if(r>0) e+=1;
	      for(int j=i;j<i+n;j++)
	      {
	        if(e>0)
            {
                a[i][j%n]=1;
                e--;
            }
	      }
	      r--;
	  }
	  int maxir=0;
	  int minir=n;
	  int maxic=0;
	  int minic=n;
	  int sum=0;
	  for(int i=0;i<n;i++)
	  {
	      sum=0;
	      for(int j=0;j<n;j++)
	      {
	         if(a[i][j]==1) sum++;
	      }
	      maxir=max(maxir,sum);
	      minir=min(minir,sum);
	  }
	  for(int i=0;i<n;i++)
	  {
	      sum=0;
	      for(int j=0;j<n;j++)
	      {
	         if(a[j][i]==1) sum++;
	      }
	      maxic=max(maxic,sum);
	      minic=min(minic,sum);
	  }
	  cout<<((maxir-minir)*(maxir-minir))+((maxic-minic)*(maxic-minic))<<endl;
	  for(int i=0;i<n;i++)
	  {
	      for(int j=0;j<n;j++)
	      {
	          cout<<a[i][j];
	      }
	      cout<<endl;
	  }

	}


	return 0;
}


Comments

Submit
0 Comments
More Questions

136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction